package com.jzo2o.customer.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.DBException;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.mapper.AgencyCertificationAuditMapper;
import com.jzo2o.customer.mapper.AgencyCertificationMapper;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.AgencyCertificationAuditResDTO;
import com.jzo2o.customer.service.IAgencyCertificationAuditService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 机构认证审核表 服务实现类
 * </p>
 *
 * @author mr.zhao
 * @since 2025-07-01
 */
@Service
public class AgencyCertificationAuditServiceImpl extends ServiceImpl<AgencyCertificationAuditMapper, AgencyCertificationAudit> implements IAgencyCertificationAuditService {

	@Autowired
	private AgencyCertificationMapper agencyCertificationMapper;

	/**
	 * 查询驳回原因
	 *
	 * @param userId
	 * @return
	 */
	@Override
	public String getRejectReason(Long userId) {
		AgencyCertificationAudit audit = this.lambdaQuery().eq(AgencyCertificationAudit::getServeProviderId, userId)
				.eq(AgencyCertificationAudit::getAuditStatus, 1)
				.orderByDesc(AgencyCertificationAudit::getUpdateTime)
				.last("limit 1")
				.one();
		String rejectReason = audit.getRejectReason();
		return rejectReason;
	}

	/**
	 * 分页查询审核信息
	 *
	 * @param dto
	 * @return
	 */
	@Override
	public PageResult<AgencyCertificationAuditResDTO> pageList(AgencyCertificationAuditPageQueryReqDTO dto) {
		PageResult<AgencyCertificationAuditResDTO> pageResult = PageHelperUtils.selectPage(dto, () -> baseMapper.pageList());
		return pageResult;
	}

	/**
	 * 审核机构认证信息
	 *
	 * @param certificationStatus
	 * @param id
	 * @param rejectReason
	 */
	@Transactional
	@Override
	public void audit(Integer certificationStatus, Long id, String rejectReason) {
		//1. 参数校验
		if (certificationStatus == null && id == null){
			throw new ForbiddenOperationException("参数非法");
		}

		//2. 审核
		boolean result = this.lambdaUpdate().set(AgencyCertificationAudit::getCertificationStatus, certificationStatus)
				.set(AgencyCertificationAudit::getRejectReason, rejectReason)
				.set(AgencyCertificationAudit::getAuditStatus,1)
				.set(AgencyCertificationAudit::getAuditorId, UserContext.currentUserId())
				.set(AgencyCertificationAudit::getAuditorName, UserContext.currentUser().getName())
				.set(AgencyCertificationAudit::getAuditTime, LocalDateTime.now())
				.eq(AgencyCertificationAudit::getId, id)
				.update();
		if (!result){
			throw new DBException("审核失败");
		}

		//3. 修改认证信息表
		AgencyCertification agencyCertification = new AgencyCertification();
		AgencyCertificationAudit audit = this.lambdaQuery().eq(AgencyCertificationAudit::getId, id).one();
		agencyCertification.setId(audit.getServeProviderId());
		agencyCertification.setCertificationStatus(certificationStatus);
		int i = agencyCertificationMapper.updateById(agencyCertification);
		if (i == 0){
			throw new DBException("修改认证信息表失败");
		}
	}
}
